/********************************************************************************

Created Date: 28th December 2011.
Purpose: The trigger is responsible for creating New Order records when a new Digital Express record is inserted.
Author: Arun Ramachandran.
Version 1.0

Update: [Arun[1/9]] - Updated API version from 23.0 to 22.0
Update: [Arun[1/27]]- Updated Office code input to Order from Digital Express Record.

********************************************************************************/

trigger OOROrderCreation on Digital_Express__c (After insert) {

    
    List<Opportunity> NewOppty = New List<Opportunity>();
    List<Digital_Express__c> NewRecords = Trigger.New;
    Set<Id> UserSet = new Set<Id>();
    Map<Id,User> UserMap = new map<Id,User>();  
    Map<Id,Id> OppDEMap = new Map<Id,Id>();
    
    ID SFAOORRenewalrecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
    
    Account acct = new Account();                    
    acct = [Select Id,Primary_Phone__c,OOR_Secondary_Account_Owner__c from Account where Name =: 'OOR Digital Express' Limit 1];
        
        for(Digital_Express__c DigRec : NewRecords){
                UserSet.add(DigRec.Owner_Rep_Name__c);
                UserSet.add(DigRec.Manager_of_Rep__c);
        }
        
        for(User usr:[Select id,firstname,lastname from User where Id IN: UserSet]){
        UserMap.put(usr.Id,usr);
        }
        
        for(Digital_Express__c DigRec : NewRecords){
            Date myDate = Date.Today();
            String myString1 = String.ValueOf(myDate);
            String NameDate = myString1.Substring(0,10);  // Code to remove 00:00:00 from the Opportunity name
            String ManagerName;
            String UserName=''; 
            If(DigRec.Owner_Rep_Name__c!=null){
             UserName= UserMap.get(DigRec.Owner_Rep_Name__c).FirstName+' '+UserMap.get(DigRec.Owner_Rep_Name__c).LastName;
            }
            // Code to add Manager of Rep name to the Order
            
            if(DigRec.Manager_of_Rep__c != NULL){
            ManagerName = UserMap.get(DigRec.Manager_of_Rep__c).FirstName+' '+UserMap.get(DigRec.Manager_of_Rep__c).LastName;
            }
             ListViewIds__c adminId = ListViewIds__c.getInstance();  

             Opportunity Opp = new Opportunity(
               
               //Opportunity field details.
                RecordTypeId = SFAOORRenewalrecordTypeID,
                Name = 'OOR Digital Express'+' '+UserName+''+NameDate,
               //Commented  for resolve Active inactive Owner Issue[SZ]
               // OwnerId = DigRec.Owner_Rep_Name__c,
               //Start : - Added  for resolve Active inactive Owner Issue[SZ]
                OwnerId=adminId.SalesforceAdmin__c,
                //OwnerId= DigRec.OwnerId,
               // End  :- Added  for resolve Active inactive Owner Issue[SZ]
                Ext_Secondary_Owner__c=DigRec.Secondary_Owner__c,
                Opportunity_Type__c = 'Digital Express',                
                StageName = 'Closed',
                Probability = 100,
                Revenue_mo__c = DigRec.Monthly_Amount__c,
                CloseDate = DigRec.Completed_Date__c,
                Order_ID_SOLO__c = '9999999',
                SOLO_Contract_Revenue_mo__c = DigRec.Monthly_Amount__c,
                
                SOLO_Prev_Monthly_Amt__c = DigRec.Monthly_Amount__c,
                Order_Date_SOLO__c = DigRec.Completed_Date__c,
                SOLO_Contract_Date__c = DigRec.Completed_Date__c,
                Order_Completion_Date__c = DigRec.Completed_Date__c,
                Projected_Close_Date__c = DigRec.Completed_Date__c,
                SOLO_Submitted_Date__c= DigRec.Completed_Date__c,
                SOLO_Order_State__c ='C',
                LeadSource = '',
                AccountId = acct.Id,
                Monthly_Amount__c = DigRec.Monthly_Amount__c,
                
                SOLO_Order_Value__c = DigRec.Total_Amount__c,
                SOLO_Prev_Order_Value__c = 0,
                //Secondary_Owner__c=DigRec.Secondary_Owner__c,
                
                //Start : - Added  for resolve Active inactive Owner Issue[SZ]             
                Ext_Primary_Owner__c=DigRec.Owner_Rep_Name__c,
                 // End  :- Added  for resolve Active inactive Owner Issue[SZ]
                SOLO_Manager_P_Owner__c = ManagerName,
                Digital_Express_ID__c = DigRec.Id,
                Digital_Express_mo__c = DigRec.Monthly_Amount__c,
                Digital_Express_Orders_Count__c = 1,
                SOLO_Office_P_Owner_Code__c = DigRec.Office_Code__c    //Update added on 1/27 by Arun Ramachandran                          
                             
                );
                //system.debug('ID for Opty' + DigRec.Secondary_Owner__c);
                NewOppty.add(Opp);
                }
                         
                                
         if(NewOppty.size()>0){
              Try{                  
                              insert NewOppty;
                              for(Opportunity Opp: NewOppty){
                                  
                                  OppDEMap.put(Opp.Digital_Express_ID__c,Opp.ID);
                              } 
                }
              Catch(DMLException e){
                  system.debug('DML Exception'+e);
              }
            }
    
}